<?php

class Permintaan extends CI_Controller {
	
	function Permintaan()
	{
		parent::__construct();
		$this->load->model('titik_penjualan_model', '', TRUE);
		$this->load->model('Home_model', '', TRUE);
		$this->load->model('auth_model', '', TRUE);
		$this->load->model('lokasi_model', '', TRUE);
		$this->load->model('satuan_model', '', TRUE);
		$this->load->model('sapi_model');
		$this->load->model('permintaan_model');
	}
	
	function index()
	{
		if ($this->session->userdata('login') == FALSE){
			redirect('auth');
		}else{
			$id_role = $this->session->userdata('id_role'); 
			$data = array(
				"main_view" => "permintaan/konten",
				"title"		=> "Permintaan Produk Sapi",
				"nama_user"	=> "Admin",
				
				"provinsi"	=> $this->permintaan_model->get_provinsi(),
				"permintaan"=> $this->permintaan_model->get_permintaan(),
				//"sum_rph"	=> $this->permintaan_model->get_sum_produksi_rph(),
				//"sum_permintaan"	=> $this->permintaan_model->get_sum_permintaan(),
				"all_sapi"	=> $this->sapi_model->get_all_sapi(),
				"hak_akses0"=> $this->Home_model->get_hak_akses($id_role,0),
				"hak_akses1"=> $this->Home_model->get_hak_akses($id_role,1),
				"hak_akses2"=> $this->Home_model->get_hak_akses($id_role,2)
			);
			//var_dump($data['sum_permintaan']->result_array());
			$this->load->view('template', $data);
		}
		
	}
	
	function insert_permintaan(){
		$tanggal 	= explode("/", $this->input->post("tanggal-kejadian"));
		$tgl		= $tanggal[2]."-".$tanggal[0]."-".$tanggal[1];
		$permintaan = array(
			"ID_SAPI"			=> $this->input->post("prod-sapi"),
			"ID_PROV"			=> $this->input->post("provinsi"),
			"JUMLAH"			=> $this->input->post("jumlah"),
			"TANGGAL_KEJADIAN"	=> $tgl,
			"KOMENTAR"			=> $this->input->post("catatan")
		);
		
		//var_dump($permintaan);
		if($this->permintaan_model->insert_permintaan($permintaan)){
			redirect("/permintaan");
		} else {
			echo "gagal";
		}
	}
	
	function add_titik_penjualan(){
	
		$tanggal = $this->input->post('tanggal_berdiri');
		$tanggal_arr = explode('/', $tanggal);
		$tanggal = $tanggal_arr[2]."-".$tanggal_arr[0]."-".$tanggal_arr[1];
	
		$arr = array(
			'NAMA' => $this->input->post('nama'),
			'ALAMAT' => $this->input->post('alamat'),
			'ID_KOTA' => $this->input->post('id_kota'),
			'NO_TELP' => $this->input->post('no_telp'),
			'EMAIL' => $this->input->post('email'),
			'KODE_POS' => $this->input->post('kode_pos'),
			'TANGGAL_BERDIRI' => $tanggal,
			'LAT' => $this->input->post('lat'),
			'LON' => $this->input->post('lng'),
			'INFO' => $this->input->post('info')
		);
		
		$id_titik_pasok = $this->titik_penjualan_model->insert_titik_pasok($arr);
		
		$arr2 = array(
			'ID_TITIK_PASOK' => $id_titik_pasok,
			'ID_SATUAN' => $this->input->post('id_satuan'),
			'PENJUALAN_NORMAL' => $this->input->post('penjualan_normal'),
		);
		
		$this->titik_penjualan_model->insert_titik_penjualan($arr2);
		
	}
	
	function update_titik_penjualan(){
		$id1 = $this->input->post('id_titik_penjualan');
		
		$tanggal = $this->input->post('tanggal_berdiri');
		$tanggal_arr = explode('/', $tanggal);
		if(count($tanggal_arr) > 1) $tanggal = $tanggal_arr[2]."-".$tanggal_arr[0]."-".$tanggal_arr[1];
	
		$arr1 = array(
			'NAMA' => $this->input->post('nama'),
			'ALAMAT' => $this->input->post('alamat'),
			'ID_KOTA' => $this->input->post('id_kota'),
			'NO_TELP' => $this->input->post('no_telp'),
			'EMAIL' => $this->input->post('email'),
			'KODE_POS' => $this->input->post('kode_pos'),
			'TANGGAL_BERDIRI' => $tanggal,
			'LAT' => $this->input->post('lat'),
			'LON' => $this->input->post('lng'),
			'INFO' => $this->input->post('info')
		);
		
		$titik_penjualan = $this->titik_penjualan_model->get_titik_penjualan($id1);
		foreach($titik_penjualan->result_array() as $row){
			$id2 = $row['ID_TITIK_PASOK'];
		}
		
		$arr2 = array(
			'ID_SATUAN' => $this->input->post('id_satuan'),
			'PENJUALAN_NORMAL' => $this->input->post('penjualan_normal'),
		);
		
		//var_dump($arr1);
		$this->titik_penjualan_model->update_titik_penjualan($id2, $id1, $arr1, $arr2);
		
	}
	
	function delete_titik_penjualan(){
		$id_jual = $this->input->post('id_titik_penjualan');
		$id_pasok = $this->input->post('id_titik_pasok');
		$this->titik_penjualan_model->delete_titik_penjualan($id_jual, $id_pasok);
	}
	
	function update_role(){
		$id = $this->input->post('id_role');
		$data['role_res'] = $this->role_model->get_role($id);
		$this->load->view('role/role_edit', $data);
		
	}
	
	function submit_role(){
		$id = $this->input->post('id_role');
		$arr = array(
			'NAMA_ROLE' => $this->input->post('nama_role')
		);
		
		$this->role_model->update_role($id, $arr);
		
		redirect('role');
	}
	
	
	
}